home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Net / IPv6.php < prev    next >
Encoding:
PHP Script  |  2005-09-26  |  7.1 KB  |  219 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Merz <alexander.merz@web.de>                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: IPv6.php,v 1.12 2005/09/01 12:42:00 alexmerz Exp $
  20.  
  21. /**
  22. * Class to validate and to work with IPv6
  23. *
  24. * @author  Alexander Merz <alexander.merz@t-online.de>
  25. * @author elfrink at introweb dot nl
  26. * @package Net_IPv6
  27. * @version $Id: IPv6.php,v 1.12 2005/09/01 12:42:00 alexmerz Exp $
  28. * @access  public
  29. */
  30. class Net_IPv6 {
  31.  
  32.     // {{{ Uncompress()
  33.  
  34.     /**
  35.      * Uncompresses an IPv6 adress
  36.      *
  37.      * RFC 2373 allows you to compress zeros in an adress to '::'. This
  38.      * function expects an valid IPv6 adress and expands the '::' to
  39.      * the required zeros.
  40.      *
  41.      * Example:  FF01::101    ->  FF01:0:0:0:0:0:0:101
  42.      *           ::1        ->  0:0:0:0:0:0:0:1
  43.      *
  44.      * @access public
  45.      * @see Compress()
  46.      * @static
  47.      * @param string $ip    a valid IPv6-adress (hex format)
  48.      * @return string    the uncompressed IPv6-adress (hex format)
  49.      */
  50.     function Uncompress($ip) {
  51.         $uip = $ip;
  52.         $c1 = -1;
  53.         $c2 = -1;
  54.         if (false !== strpos($ip, '::') ) {
  55.             list($ip1, $ip2) = explode('::', $ip);
  56.             if(""==$ip1) {
  57.                 $c1 = -1;
  58.             } else {
  59.                    $pos = 0;
  60.                 if(0 < ($pos = substr_count($ip1, ':'))) {
  61.                     $c1 = $pos;
  62.                 } else {
  63.                     $c1 = 0;
  64.                 }
  65.             }
  66.             if(""==$ip2) {
  67.                 $c2 = -1;
  68.             } else {
  69.                 $pos = 0;
  70.                 if(0 < ($pos = substr_count($ip2, ':'))) {
  71.                     $c2 = $pos;
  72.                 } else {
  73.                     $c2 = 0;
  74.                 }
  75.             }
  76.             if(strstr($ip2, '.')) {
  77.                 $c2++;
  78.             }
  79.             if(-1 == $c1 && -1 == $c2) { // ::
  80.                 $uip = "0:0:0:0:0:0:0:0";
  81.             } else if(-1==$c1) {              // ::xxx
  82.                 $fill = str_repeat('0:', 7-$c2);
  83.                 $uip =  str_replace('::', $fill, $uip);
  84.             } else if(-1==$c2) {              // xxx::
  85.                 $fill = str_repeat(':0', 7-$c1);
  86.                 $uip =  str_replace('::', $fill, $uip);
  87.             } else {                          // xxx::xxx
  88.                 $fill = str_repeat(':0:', 6-$c2-$c1);
  89.                 $uip =  str_replace('::', $fill, $uip);
  90.                 $uip =  str_replace('::', ':', $uip);
  91.             }
  92.         }
  93.         return $uip;
  94.     }
  95.  
  96.     // }}}
  97.     // {{{ Compress()
  98.  
  99.     /**
  100.      * Compresses an IPv6 adress
  101.      *
  102.      * RFC 2373 allows you to compress zeros in an adress to '::'. This
  103.      * function expects an valid IPv6 adress and compresses successive zeros
  104.      * to '::'
  105.      *
  106.      * Example:  FF01:0:0:0:0:0:0:101     -> FF01::101
  107.      *           0:0:0:0:0:0:0:1        -> ::1
  108.      *
  109.      * @access public
  110.      * @see Uncompress()
  111.      * @static
  112.      * @param string $ip    a valid IPv6-adress (hex format)     
  113.      * @return string    the compressed IPv6-adress (hex format)
  114.      * @author elfrink at introweb dot nl
  115.      */
  116.     function Compress($ip)    {
  117.         $cip = $ip;
  118.  
  119.         if (!strstr($ip, '::')) {
  120.              $ipp = explode(':',$ip);
  121.              for($i=0; $i<count($ipp); $i++) {
  122.                  $ipp[$i] = dechex(hexdec($ipp[$i]));
  123.              }
  124.             $cip = ':' . join(':',$ipp) . ':';
  125.             preg_match_all("/(:0)+/", $cip, $zeros);
  126.             if (count($zeros[0])>0) {
  127.                 $match = '';
  128.                 foreach($zeros[0] as $zero) {
  129.                     if (strlen($zero) > strlen($match))
  130.                         $match = $zero;
  131.                 }
  132.                 $cip = preg_replace('/' . $match . '/', ':', $cip, 1);
  133.             }
  134.             $cip = preg_replace('/((^:)|(:$))/', '' ,$cip);
  135.             $cip = preg_replace('/((^:)|(:$))/', '::' ,$cip);
  136.          }
  137.          return $cip;
  138.     }
  139.  
  140.     // }}}
  141.     // {{{ SplitV64()
  142.  
  143.     /**
  144.      * Splits an IPv6 adress into the IPv6 and a possible IPv4 part
  145.      *
  146.      * RFC 2373 allows you to note the last two parts of an IPv6 adress as
  147.      * an IPv4 compatible adress
  148.      *
  149.      * Example:  0:0:0:0:0:0:13.1.68.3
  150.      *           0:0:0:0:0:FFFF:129.144.52.38
  151.      *
  152.      * @access public
  153.      * @static
  154.      * @param string $ip    a valid IPv6-adress (hex format)
  155.      * @return array        [0] contains the IPv6 part, [1] the IPv4 part (hex format)
  156.      */
  157.     function SplitV64($ip) {
  158.         $ip = Net_IPv6::Uncompress($ip);
  159.         if (strstr($ip, '.')) {
  160.             $pos = strrpos($ip, ':');
  161.             $ip{$pos} = '_';
  162.             $ipPart = explode('_', $ip);
  163.             return $ipPart;
  164.         } else {
  165.             return array($ip, "");
  166.         }
  167.     }
  168.  
  169.     // }}}
  170.     // {{{ checkIPv6
  171.  
  172.     /**
  173.      * Checks an IPv6 adress
  174.      *
  175.      * Checks if the given IP is IPv6-compatible
  176.      *
  177.      * @access public
  178.      * @static
  179.      * @param string $ip    a valid IPv6-adress
  180.      * @return boolean    true if $ip is an IPv6 adress
  181.      */
  182.     function checkIPv6($ip) {
  183.  
  184.         $ipPart = Net_IPv6::SplitV64($ip);
  185.         $count = 0;
  186.         if (!empty($ipPart[0])) {
  187.             $ipv6 =explode(':', $ipPart[0]);
  188.             for ($i = 0; $i < count($ipv6); $i++) {
  189.                 $dec = hexdec($ipv6[$i]);
  190.                 $hex = strtoupper(preg_replace("/^[0]{1,3}(.*[0-9a-fA-F])$/", "\\1", $ipv6[$i]));
  191.                 if ($ipv6[$i] >= 0 && $dec <= 65535 && $hex == strtoupper(dechex($dec))) {
  192.                     $count++;
  193.                 }
  194.             }
  195.             if (8 == $count) {
  196.                 return true;
  197.             } elseif (6 == $count and !empty($ipPart[1])) {
  198.                 $ipv4 = explode('.',$ipPart[1]);
  199.                 $count = 0;
  200.                 for ($i = 0; $i < count($ipv4); $i++) {
  201.                     if ($ipv4[$i] >= 0 && (integer)$ipv4[$i] <= 255 && preg_match("/^\d{1,3}$/", $ipv4[$i])) {
  202.                         $count++;
  203.                     }
  204.                 }
  205.                 if (4 == $count) {
  206.                     return true;
  207.                 }
  208.             } else {
  209.                 return false;
  210.             }
  211.  
  212.         } else {
  213.             return false;
  214.         }
  215.     }
  216.     // }}}
  217. }
  218. ?>
  219.